package com.zg.ktcomwan.adapter

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import androidx.navigation.Navigation
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zg.ktcomwan.R
import com.zg.ktcomwan.databinding.ArticleItemBinding
import com.zg.ktcomwan.databinding.ProjectListItemBinding
import com.zg.ktcomwan.model.ArticleBean

/***
 * @Author:      zxd
 * @CreateDate:  2021/8/10 9:30
 * @Description: 首页文章的适配器
 */
class SquareAdapter(private val context: Context) :
    PagingDataAdapter<ArticleBean, SquareAdapter.ViewHolder>(ArticleDiffCallback()) {

    class ViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bindData(item: ArticleBean, position: Int, context: Context) {
            val binding: ArticleItemBinding = binding as ArticleItemBinding
            binding.setArticle(item)
            binding.setContext(context)
            binding.root.setOnClickListener {
                val bundle = Bundle()
                bundle.putString("url", item.link)
                bundle.putString("title", item.title)
                Navigation.findNavController(it).navigate(R.id.navigation_web, bundle)
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        getItem(position)?.let { holder.bindData(it, holder.layoutPosition, context) };
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = DataBindingUtil.inflate<ViewDataBinding>(
            LayoutInflater.from(parent.context),
            viewType,
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun getItemViewType(position: Int): Int {
        return R.layout.article_item
    }
}